package com.ethink.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.warehouse.api.info.MateInventoryDetailOrderInfo;
import com.ethink.warehouse.api.info.MateInventoryDetailOrderMoreInfo;
import com.ethink.warehouse.api.request.MateInventoryDetailOrderAddRequest;
import com.ethink.warehouse.api.request.MateInventoryDetailOrderQueryRequest;
import com.ethink.warehouse.api.request.MateInventoryDetailOrderUpdateRequest;
import com.ethink.warehouse.api.response.MateInventoryDetailOrderAddResponse;
import com.ethink.warehouse.api.response.MateInventoryDetailOrderDeleteResponse;
import com.ethink.warehouse.api.response.MateInventoryDetailOrderQueryResponse;
import com.ethink.warehouse.api.response.MateInventoryDetailOrderUpdateResponse;
import com.ethink.warehouse.common.entity.MateInventoryDetailOrder;
import com.ethink.warehouse.common.mapper.MateInventoryDetailOrderMapper;
import com.ethink.warehouse.converter.MateInventoryDetailOrderConverter;
import com.ethink.warehouse.service.MateInventoryDetailOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 入库/出库明细单 Service接口实现
 *
 * @author default
 * @date 2022-12-15
 */
@Slf4j
@Service
public class MateInventoryDetailOrderServiceImpl extends ServiceImpl<MateInventoryDetailOrderMapper, MateInventoryDetailOrder> implements MateInventoryDetailOrderService {

    @Autowired
    private MateInventoryDetailOrderMapper mateInventoryDetailOrderMapper;

    @Autowired
    private MateInventoryDetailOrderConverter mateInventoryDetailOrderConverter;

    @Override
    public Page<MateInventoryDetailOrderQueryResponse> queryMateInventoryDetailOrder(MateInventoryDetailOrderQueryRequest request) {
        return mateInventoryDetailOrderMapper.queryMateInventoryDetailOrder(PageUtils.buildPage(request), request);
    }

    @Override
    public MateInventoryDetailOrderInfo getMateInventoryDetailOrderById(Long mateInventoryDetailOrderId) {
        return mateInventoryDetailOrderMapper.getMateInventoryDetailOrderById(mateInventoryDetailOrderId);
    }

    @Override
    public List<MateInventoryDetailOrderMoreInfo> listMateInventoryDetailOrder(MateInventoryDetailOrderQueryRequest request) {
        return mateInventoryDetailOrderMapper.listMateInventoryDetailOrder(request);
    }

    @Override
    public MateInventoryDetailOrderAddResponse addMateInventoryDetailOrder(MateInventoryDetailOrderAddRequest request) {
        MateInventoryDetailOrderAddResponse response = new MateInventoryDetailOrderAddResponse();
        MateInventoryDetailOrder mateInventoryDetailOrder = new MateInventoryDetailOrder();
        BeanUtils.copyProperties(request, mateInventoryDetailOrder);
        // FIXME set默认字段
        mateInventoryDetailOrderMapper.insert(mateInventoryDetailOrder);
        return response;
    }

    @Override
    public MateInventoryDetailOrderUpdateResponse updateMateInventoryDetailOrder(MateInventoryDetailOrderUpdateRequest request) {
        MateInventoryDetailOrderUpdateResponse response = new MateInventoryDetailOrderUpdateResponse();
        LambdaUpdateWrapper<MateInventoryDetailOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryDetailOrder::getId, request.getId());
        // FIXME 按实际业务需求set
        updateWrapper.set(MateInventoryDetailOrder::getSourceDetailOrderNo, request.getSourceDetailOrderNo());
        updateWrapper.set(MateInventoryDetailOrder::getMateInventoryId, request.getMateInventoryId());
        updateWrapper.set(MateInventoryDetailOrder::getMateSku, request.getMateSku());
        updateWrapper.set(MateInventoryDetailOrder::getInventoryType, request.getInventoryType());
        updateWrapper.set(MateInventoryDetailOrder::getSaleSubChannel, request.getSaleSubChannel());
        updateWrapper.set(MateInventoryDetailOrder::getPurchaseCode, request.getPurchaseCode());
        updateWrapper.set(MateInventoryDetailOrder::getProduceCode, request.getProduceCode());
        updateWrapper.set(MateInventoryDetailOrder::getCountryOfOrigin, request.getCountryOfOrigin());
        updateWrapper.set(MateInventoryDetailOrder::getProductDate, request.getProductDate());
        updateWrapper.set(MateInventoryDetailOrder::getExpireDate, request.getExpireDate());
        updateWrapper.set(MateInventoryDetailOrder::getActualInQty, request.getActualInQty());
        updateWrapper.set(MateInventoryDetailOrder::getPurchaseOrderNo, request.getPurchaseOrderNo());
        updateWrapper.set(MateInventoryDetailOrder::getPurchaseDetailOrderNo, request.getPurchaseDetailOrderNo());
        updateWrapper.set(MateInventoryDetailOrder::getCostPrice, request.getCostPrice());
        mateInventoryDetailOrderMapper.update(null, updateWrapper);
        return response;
    }

    @Override
    public MateInventoryDetailOrderDeleteResponse deleteMateInventoryDetailOrder(Long mateInventoryDetailOrderId) {
        MateInventoryDetailOrderDeleteResponse response = new MateInventoryDetailOrderDeleteResponse();
        LambdaUpdateWrapper<MateInventoryDetailOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryDetailOrder::getId, mateInventoryDetailOrderId);
        updateWrapper.set(MateInventoryDetailOrder::getIsDeleted, true);
        mateInventoryDetailOrderMapper.update(null, updateWrapper);
        return response;
    }

}
